package com.pbd.client;

import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

public class MemberOverView extends Composite {
	private Map memberMap;

	public MemberOverView(Map memberMap) {
		this.memberMap = memberMap;
		initUI();
	}

	private void initUI() {
		FlexTable mainTable = new FlexTable();

		FlexTable topTable = new FlexTable();
		topTable.setStyleName("blue_box_hd");
		String name = (String) memberMap.get("Name");
		String address = (String) memberMap.get("Address");
		String phone = (String) memberMap.get("Phone");
		String fax = (String) memberMap.get("Fax");
		String mobile = (String) memberMap.get("Mobile");
		String email = (String) memberMap.get("Email");
		String location = (String) memberMap.get("Area");
		String postedOn = (String) memberMap.get("PostedOn");
		String partyID = (String) memberMap.get("Id");
		String logoPath = (String) memberMap.get("LogoPath");
		topTable.setWidget(0, 0, new Image(".\\images\\arrow_02.png"));

		topTable.setWidget(0, 1, new HTML(name));
		topTable.getFlexCellFormatter().setStyleName(0, 1, "heading_white1");
		topTable.getFlexCellFormatter().setWidth(0, 0, "40px");
		topTable.getFlexCellFormatter().setWidth(0, 1, "620px");

		FlexTable centerTable = new FlexTable();
		if (logoPath == null)
			centerTable.setWidget(0, 0, new Image(".\\images\\photo_un.gif"));
		else{
			Image logoImage = new Image("." + logoPath);
			logoImage.setWidth("88px");
			logoImage.setHeight("88px");
			centerTable.setWidget(0, 0, logoImage);
		}

		StringBuffer sbContent = new StringBuffer();
		sbContent.append("<p class=\"body_txt_black\">");
		if (address != null && address.trim().length() > 0)
			sbContent.append(address);
		if (phone != null && phone.trim().length() > 0) {
			sbContent.append("<br> <span class=\"body_txt_blue2\">Phone :</span>");
			sbContent.append(phone);
		}
		if (fax != null && fax.trim().length() > 0) {
			sbContent.append("<br> <span class=\"body_txt_blue2\">Fax :</span>");
			sbContent.append(fax);
		}
		if (mobile != null && mobile.trim().length() > 0) {
			sbContent.append("<br> <span class=\"body_txt_blue2\">Mobile :</span>");
			sbContent.append(mobile);
		}

		if (email != null && email.trim().length() > 0) {
			sbContent.append("<br> <span class=\"body_txt_blue2\">Email :</span>");
			sbContent.append("<a href=\"mailto:" + email + "\" class=\"hyperLnk1\">" + email + "</a>");
		}
		sbContent.append("</p>");
		centerTable.setWidget(0, 1, new HTML(sbContent.toString()));
		centerTable.getFlexCellFormatter().setStyleName(0, 1, "blank_box");

		FlexTable bottomTable = new FlexTable();
		bottomTable.setWidget(0, 0, new Image(".\\images\\view_details_icon.png"));

		HTML viewDetailHtml = new HTML("<a href=\"#\">View Details</a>");
		viewDetailHtml.setLayoutData(partyID);
		bottomTable.setWidget(0, 1, viewDetailHtml);
		addHtmlClickHandler(viewDetailHtml);
		bottomTable.setWidget(0, 2, new Image(".\\images\\pin_icon.png"));
		bottomTable.setWidget(0, 3, new HTML("<span class=\"body_txt_orange\">Location: </span> <span class=\"body_txt_black_b\">" + location + "</span>"));
		bottomTable.setWidget(0, 4, new Image(".\\images\\posted_icon.png"));
		bottomTable.setWidget(0, 5, new HTML("<span class=\"body_txt_orange\">Posted On: </span> <span class=\"body_txt_black_b\">" + postedOn + "</span>"));
		bottomTable.getFlexCellFormatter().setWidth(0, 0, "18px");
		bottomTable.getFlexCellFormatter().setWidth(0, 1, "90px");
		bottomTable.getFlexCellFormatter().setStyleName(0, 1, "hyperLnk");
		bottomTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		bottomTable.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		bottomTable.setCellSpacing(5);

		mainTable.setWidget(0, 0, topTable);
		mainTable.setWidget(1, 0, centerTable);
		mainTable.setWidget(2, 0, bottomTable);
		initWidget(mainTable);
	}

	private void addHtmlClickHandler(final HTML html) {
		html.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				FlexTable mainTable = (FlexTable) RootPanel.get("mainContainer").getWidget(0);
				mainTable.clear();
				mainTable.setCellSpacing(8);
				mainTable.setWidget(0, 0, new MemberFullView(Long.valueOf((String) html.getLayoutData())));

				FlexTable adsTable = new FlexTable();
				adsTable.setStyleName("gray_box");
				adsTable.setWidth("250px");
				adsTable.setHeight("757px");
				adsTable.setWidget(0, 0, new HTML("<p><span class=\"body_txt_black\"></span><span class=\"heading_blue_s2\"><br></span><span class=\"heading_blue\">ADVERTISE<br>YOUR<br>BUSINESS</span><span class=\"body_txt_black1\"><br><br></span><span class=\"heading_orange\"><font color=\"#00e1ff\" size=\"+4\"><b>HERE</b></font></span><br><br></p><p class=\"heading_orange1\"><span class=\"body_txt_black\">Please Contact:</span><br><img src=\".\\images\\phone_icon.png\"/>&nbsp&nbsp<span class=\"heading_orange1\">6529 1930 / 2235 0986</span></p>"));
				mainTable.setWidget(0, 1, adsTable);
			}
		});
	}
}
